package com.loiot.baqi.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.loiot.baqi.commons.message.email.EmailClient;
import com.loiot.baqi.commons.message.email.SimpleEmailVo;
import com.loiot.baqi.constant.ApplicationConst;
import com.loiot.baqi.constant.Const;
import com.loiot.baqi.constant.DictionaryUtil;
import com.loiot.baqi.controller.response.AjaxResponse;
import com.loiot.baqi.controller.response.Pager;
import com.loiot.baqi.dao.AccountDao;
import com.loiot.baqi.dao.BaseDao;
import com.loiot.baqi.dao.ZpJlExpandInfoDao;
import com.loiot.baqi.dao.ZpJobMatchingInfoDao;
import com.loiot.baqi.dao.ZpRecommendFlowInfoDao;
import com.loiot.baqi.dao.ZpRecommendFlowRollbackListDao;
import com.loiot.baqi.service.ZpRecommendFlowInfoService;
import com.loiot.baqi.status.AccountType;
import com.loiot.baqi.status.CompanyInterfaceRollbackType;
import com.loiot.baqi.status.DictionaryType;
import com.loiot.baqi.status.JlAuditType;
import com.loiot.baqi.status.JlFlowType;
import com.loiot.baqi.status.PauseStartType;
import com.loiot.baqi.status.AccountType;
import com.loiot.baqi.status.RecommendFlowType;
import com.loiot.baqi.utils.SpringBeanFactoryUtils;
import com.loiot.baqi.utils.UserSessionUtils;
import com.loiot.baqi.utils.webSocket.ChatAnnotation;
import com.loiot.baqi.pojo.ZpJlExpandInfo;
import com.loiot.baqi.pojo.ZpJlInfo;
import com.loiot.baqi.pojo.ZpJlStatisticsInfo;
import com.loiot.baqi.pojo.ZpRecommendFlowInfo;
import com.loiot.baqi.pojo.ZpJlJobLevels;
import com.loiot.baqi.pojo.ZpRecommendFlowRollbackList;
import com.loiot.commons.utils.DateUtil;
import com.loiot.commons.utils.StringUtil;
import com.timeloit.pojo.Account;


/**
 * 推荐流程 逻辑类。
 * 
 * @author  wangzx 
 * @creation 2015-11-20
 */
@Service("zpRecommendFlowInfoService")
@Transactional
public class ZpRecommendFlowInfoService{
    
    private Logger log = LoggerFactory.getLogger(this.getClass());
    
    @Resource
	private ZpRecommendFlowInfoDao zpRecommendFlowInfoDao;
    
    @Resource
	private ZpJobMatchingInfoDao zpJobMatchingInfoDao;
    
    @Autowired
    private EmailClient emailClient;
    
    @Resource
   	private ZpRecommendFlowRollbackListDao zpRecommendFlowRollbackListDao;

	@Resource
	private ZpJlStatisticsInfoService zpJlStatisticsInfoService;
	
	@Resource
	private BaseDao baseDao;

    /**
     * 账号数据访问接口
     */
    @Resource
    private AccountDao accountDao;
    
    @Resource
	private ZpJlExpandInfoDao zpJlExpandInfoDao;
	
	
	 /**
     * 查询 推荐流程列表分页
     * 
     * @param name 推荐流程名称
     * @param pageIndex 页索引
     * @return
     */
    public Pager<ZpRecommendFlowInfo> queryZpRecommendFlowInfoListPage(HashMap<String,Object> pMap, int pageIndex)throws Exception {

        // 查询推荐流程列表总条数
        int totalResults = zpRecommendFlowInfoDao.getZpRecommendFlowInfoListCount(pMap);

        // 构造一个分页器
        Pager<ZpRecommendFlowInfo> pager = new Pager<ZpRecommendFlowInfo>(totalResults, pageIndex);
        //pager.setPageSize(5);
        // 查询推荐流程列表
        List<ZpRecommendFlowInfo> zpRecommendFlowInfoList = zpRecommendFlowInfoDao.queryZpRecommendFlowInfoList(pMap, pager.getSkipResults(),
                pager.getMaxResults());
        pager.setData(zpRecommendFlowInfoList);
        return pager;
    }
    
    
    
    
    /**
     * 查询 推荐流程（假）分页
     * 
     * @param name 推荐流程名称
     * @param pageIndex 页索引
     * @return
     */
    public Pager<ZpRecommendFlowInfo> queryFlasePageList(HashMap<String,Object> pMap, int pageIndex)throws Exception {
    	//假分页
    	Pager<ZpRecommendFlowInfo> pager = this.setPkList(pMap,pageIndex);
    	List<ZpRecommendFlowInfo> zpRecommendFlowInfoList = zpRecommendFlowInfoDao.queryZpRecommendFlowInfoList(pMap);
        pager.setData(zpRecommendFlowInfoList);
        return pager;
    }
    
    /**
     * 设置 假分页id集合到Map中
     * @param pMap
     * @param pageIndex
     * @return
     * @throws Exception
     */
    public Pager<ZpRecommendFlowInfo> setPkList(HashMap<String,Object> pMap,int pageIndex) throws Exception{
    	  // 查询职位匹配信息列表总条数
        List<ZpRecommendFlowInfo> list = zpRecommendFlowInfoDao.queryZpRecommendFlowInfoList(pMap);
        // 构造一个分页器
        Pager<ZpRecommendFlowInfo> pager = new Pager<ZpRecommendFlowInfo>(list.size(), pageIndex, 5,list);
        List<ZpRecommendFlowInfo> idsList = pager.getCurrentPageData();
        List<Long> ids =this.getIds(idsList);
        pMap.put("ids", ids);
        return pager;
    }
	
	 /**
     * 添加 推荐流程
     * 
     * @param p 参数对象
     */
    public ZpRecommendFlowInfo addZpRecommendFlowInfo(ZpRecommendFlowInfo p)throws Exception {
       return  zpRecommendFlowInfoDao.addZpRecommendFlowInfo(p);
    }
    
    /**
     * 修改 推荐流程
     * 
     * @param p 参数对象
     */
    public void updateZpRecommendFlowInfo(ZpRecommendFlowInfo p)throws Exception {
        zpRecommendFlowInfoDao.updateZpRecommendFlowInfo(p);
    }
    
    /**
     * 修改 推荐流程
     * 
     * @param p 参数对象
     */
    public void updateZpRecommendFlowInfo(HashMap<String,Object> pMap)throws Exception {
        zpRecommendFlowInfoDao.updateZpRecommendFlowInfo(pMap);
    }
    
    /**
     * 删除  推荐流程
     * 
     * @param id 主键
     */
    public void deleteZpRecommendFlowInfo(java.lang.Long id)throws Exception {
        zpRecommendFlowInfoDao.deleteZpRecommendFlowInfo(id);
    }
    
    /**
     * 删除  推荐流程
     * 
     * @param id 主键
     */
    public void deleteZpRecommendFlowInfo(ZpRecommendFlowInfo p)throws Exception {
        zpRecommendFlowInfoDao.deleteZpRecommendFlowInfo(p);
    }
    
    /**
     * 获得  推荐流程
     * 
     * @param id 推荐流程Id
     * 
     * @return 返回与ID匹配的推荐流程
     */
    public ZpRecommendFlowInfo getZpRecommendFlowInfoById(java.lang.Long id)throws Exception {
        return  zpRecommendFlowInfoDao.getZpRecommendFlowInfoById(id);
    }
    
    /**
     * 获得  推荐流程
     * 
     * @param id 推荐流程Id
     * 
     * @return 返回与ID匹配的推荐流程
     */
    public ZpRecommendFlowInfo getZpRecommendFlowInfoById(java.lang.Long id,Long accountId)throws Exception {
        return  zpRecommendFlowInfoDao.getZpRecommendFlowInfoById(id,accountId);
    }
    
    
    /**
     * 获得  推荐流程
     * 
     * @param id 推荐流程Id
     * 
     */
    public ZpRecommendFlowInfo getZpRecommendFlowInfo(HashMap<String,Object> pMap)throws Exception {
    	return (ZpRecommendFlowInfo) zpRecommendFlowInfoDao.getZpRecommendFlowInfo(pMap);
    }
    /**
     * 获得  推荐流程
     * 
     * @param name 推荐流程名称
     * 
     * @return 返回与NAME匹配的推荐流程
     */
    public ZpRecommendFlowInfo getZpRecommendFlowInfoByName(String name)throws Exception {
        return  zpRecommendFlowInfoDao.getZpRecommendFlowInfoByName(name);
    }
    
    /**
     * 查询 推荐流程列表
     * @return 推荐流程列表
     */
    public List<ZpRecommendFlowInfo> queryZpRecommendFlowInfoList(HashMap<String,Object> pMap)throws Exception {
        return  zpRecommendFlowInfoDao.queryZpRecommendFlowInfoList(pMap);
    }
    
    /**
     * 查询 推荐流程列表
     * @return 推荐流程列表
     */
    public List<ZpRecommendFlowInfo> queryZpRecommendFlowInfoList(ZpRecommendFlowInfo p)throws Exception {
        return  zpRecommendFlowInfoDao.queryZpRecommendFlowInfoList(p);
    }
    
    /**
     * 查询  推荐流程列表条数
     * 
     * @param name 推荐流程名称
     * @return 推荐流程列表条数
     */
    
    public int getZpRecommendFlowInfoListCount(HashMap<String,Object> pMap)throws Exception {
        return  zpRecommendFlowInfoDao.getZpRecommendFlowInfoListCount(pMap);
    }
    
    /**
     * 查询id集合
     * @return
     */
    public List<Long> getIds(List<ZpRecommendFlowInfo> list) {
    	List<Long> idsList = null;
        if(list!=null && list.size()>0) {
        	idsList = new ArrayList<Long>();
        	for (ZpRecommendFlowInfo b : list) {
            	idsList.add(null);
            }
        }
        return idsList;
    }
    
    /**
     * 统计 推荐流程列表
     * 
     * @return 推荐流程列表
     */
    public List<HashMap<String,Object>> statisticsZpRecommendFlowInfoInfo(HashMap<String, Object> pMap )throws Exception {
    	return  zpRecommendFlowInfoDao.statisticsZpRecommendFlowInfoInfo(pMap);
    }
    
    
    public AjaxResponse AddFlow(ZpRecommendFlowInfo p,int flowType){
    	//技术评审，通过
    	if(RecommendFlowType.WAIT_RECOMMEND_COMPANY.getCode()==flowType){
    		return this.technicianAudit(p);
    	} else
    	//驳回给技术评审(推荐报告不合适)
    	if(RecommendFlowType.COMPANY_INTERFACE_ROOLBACK.getCode()==flowType){
    		return this.recommandRollback(p);
    	} else
		//重新填写推荐报告
    	if(RecommendFlowType.RESET_RECOMMEND.getCode()==flowType){
    		p.setFlowStatus(flowType);
    		return this.technicianAudit(p);
    	} else
		//将简历推荐到企业（更新状态）
    	if(RecommendFlowType.ALREADY_RECOMMEND_COMPANY.getCode()==flowType){
    		return this.recommandJlToCompany(p);
    	} else
    	//推荐到企业-企业反馈
		if(RecommendFlowType.RECOMMEND_COMPANY_FAILURE.getCode()==flowType){
    		return this.companyRecommandFeedback(p);
    	} else
    	//猎头通知求职者反馈
		if(RecommendFlowType.ALREADY_INVITATION_INTERVIEW_NOTIFY.getCode()==flowType){
    		return this.headhunterNotifyFeedback(p);
    	}else
    	//是否已经去面试的反馈
		if(RecommendFlowType.HUNTER_ALREAD_GOTO_INTERVIEW.getCode()==flowType){
    		return this.isGotoInterviewFeedback(p);
    	}else
    	//面试的反馈
		if(RecommendFlowType.HUNTER_INTERVIEW_PASS.getCode()==flowType){
    		return this.interviewerFeedback(p);
    	}
    	return null;
    }
    //技术评审
    public AjaxResponse technicianAudit(ZpRecommendFlowInfo p){
    	try {
    		 HashMap<String,Object> pmap = new HashMap<String,Object>();

            if(UserSessionUtils.getAccount().getType()==AccountType.TECHICAL_AUDIT.getCode()){
         	   pmap.put("matchId", p.getMatchId());
         	   pmap.put("inPerson", UserSessionUtils.getAccount().getAccountId());
         	   int count =this.zpJobMatchingInfoDao.getZpJobMatchingInfoListCount(pmap);
                if(count==0){
             	   return AjaxResponse.ILLEGAL_OPERATER;
                }
            }
            
            if(UserSessionUtils.getAccount().getType()==AccountType.TECHICAL_AUDIT.getCode() || UserSessionUtils.getAccount().getType()==AccountType.ADMIN.getCode()){
             
              //获取企业对接人
              final Account account = getCompanyInterfacePerson();
              if(account==null){
            	   return  new AjaxResponse(-1, "没找到企业对接人");
              }
              
               ZpRecommendFlowInfo newP = new ZpRecommendFlowInfo();
         	   newP.setMatchId(p.getMatchId());
        		   newP.setTechnicianAuditTime(new Date());
        		   newP.setTechnicianAuditContent(p.getTechnicianAuditContent());
        		   newP.setTechnicianAuditStatus((int)JlAuditType.AUDIT_OK.getCode());
        		   newP.setTechnicianAuditPerson(UserSessionUtils.getAccount().getAccountId());
        		   newP.setJlId(p.getJlId());
        		   newP.setCompanyJobId(p.getCompanyJobId());
        		   newP.setFlowStatus((int)RecommendFlowType.WAIT_RECOMMEND_COMPANY.getCode());
        		   newP.setEnterpriseInterfacePerson(account.getAccountId());
        		   newP.setInTime(new Date());
        		   newP.setLastUpdateTime(DateUtil.getNow());

        		   //重新填评审内容
        		   if(p.getFlowStatus()!=null && p.getFlowStatus()==RecommendFlowType.RESET_RECOMMEND.getCode()){
        			   newP.setInTime(null);
        			   newP.setFlowStatus((int)RecommendFlowType.WAIT_RECOMMEND_COMPANY.getCode());
        			   newP.setAuditId(p.getAuditId());
        			   this.zpRecommendFlowInfoDao.updateZpRecommendFlowInfo(newP);
        			   
        			   //更新驳回处理状态（已处理）
        			   pmap.clear();
        			   pmap.put("matchId", p.getMatchId());
        			   pmap.put("roolbackStatus", CompanyInterfaceRollbackType.WAIT_DEAL.getCode());
        			   List<ZpRecommendFlowRollbackList> rollbackList = this.zpRecommendFlowRollbackListDao.queryZpRecommendFlowRollbackListList(pmap);
        			   if(rollbackList!=null && rollbackList.size()>0){
        				   ZpRecommendFlowRollbackList r= rollbackList.get(0);
        				   r.setRoolbackStatus((int)CompanyInterfaceRollbackType.ALDEAL.getCode());
        				   this.zpRecommendFlowRollbackListDao.updateZpRecommendFlowRollbackList(r);
        			   }
        			   
        		   }else {
            		   this.zpRecommendFlowInfoDao.addZpRecommendFlowInfo(newP);
        		   }
        		   //更新流程状态
        		   this.updateJlExpandFlowStatus(p.getJlId(),null, JlFlowType.PROCEED.getCode());
        		   
        		   new Thread(){ 
                    	  @Override 
                    	  public void run() { 
                    		  String nickname=account.getNickName();
                    	       String email=account.getEmail();
                    	        if(email!=null && StringUtil.isEmail(email) ){
                    	         SimpleEmailVo vo = new SimpleEmailVo();
                    	            vo.addEmail(email);
                    	                     vo.setTitle("憬仪通知");
                    	                     vo.setContent(ApplicationConst.getMessage("10102", nickname,String.valueOf("1")));
                    	                     emailClient.send(vo);
                    	                     log.info("发送时间："+DateUtil.toString(DateUtil.getNow(), DateUtil.DEFAULT_LONG_FORMAT));
                    	        }
                    	  } 
                        }.start();
            
            }
     		// 添加成功
     		return AjaxResponse.OK;
     	
     	}
         catch (Exception e) {
 			e.printStackTrace();
 			 //失败
 	        return AjaxResponse.FAILED;
 		}
    }
    
    
    //驳回给技术评审(推荐报告不合适)
    public AjaxResponse recommandRollback(ZpRecommendFlowInfo p){
    	try {
    		
   		 	HashMap<String,Object> pmap = new HashMap<String,Object>();
   		 	pmap.put("flowId", p.getAuditId());
    		List<ZpRecommendFlowRollbackList> flowList = this.zpRecommendFlowRollbackListDao.queryZpRecommendFlowRollbackListList(pmap);
    		if(flowList.size()<=2){
    			p.setFlowStatus((int)RecommendFlowType.COMPANY_INTERFACE_ROOLBACK.getCode());
        		ZpRecommendFlowRollbackList roll = new ZpRecommendFlowRollbackList();
        		roll.setFlowId(p.getAuditId());
        		roll.setJlId(p.getJlId());
        		roll.setCompanyId(p.getCompanyJobId());
        		roll.setCompanyName(p.getCompanyName());
        		roll.setCompanyJobId(p.getCompanyJobId());
        		roll.setJobName(p.getCompanyJobName());
        		roll.setMatchId(p.getMatchId());
        		roll.setRoolbackStatus((int)CompanyInterfaceRollbackType.WAIT_DEAL.getCode());
        		roll.setTechnicianAuditContent(p.getTechnicianAuditContent());
        		roll.setTechnicianAuditPerson(p.getTechnicianAuditPerson());
        		roll.setTechnicianAuditTime(p.getTechnicianAuditTime());
        		roll.setJlName(p.getJlName());
        		roll.setInTime(new Date());
        		roll.setInPerson(UserSessionUtils.getAccount().getAccountId());
        		roll.setInPersonName(UserSessionUtils.getAccount().getUsername());
        		
        		zpRecommendFlowRollbackListDao.addZpRecommendFlowRollbackList(roll);
        		
        		this.updateJlExpandFlowStatus(p.getJlId(), JlFlowType.PROCEED.getCode(), p.getMatchId());
        		
        		//更新流程状态
        		ZpRecommendFlowInfo p2 = new ZpRecommendFlowInfo();
        		p2.setAuditId(p.getAuditId());
        		p2.setFlowStatus(p.getFlowStatus());
        		this.updateZpRecommendFlowInfo(p2);
        		//发送邮件通知技术评审，重新填写推荐报告
        		sendMailToTechnicalAudit(p.getTechnicianAuditPerson(),p.getJlName());
        		return AjaxResponse.OK;
    		}
    		
     	
     	}
         catch (Exception e) {
 			e.printStackTrace();
 			 //失败
 	        return AjaxResponse.FAILED;
 		}
		return AjaxResponse.FAILED;
    }
    
    //重新写的推荐报告
    public AjaxResponse resetRecommand(ZpRecommendFlowInfo p){
    	try {
    		
   		 	HashMap<String,Object> pmap = new HashMap<String,Object>();
   		 	pmap.put("flowId", p.getAuditId());
    		List<ZpRecommendFlowRollbackList> flowList = this.zpRecommendFlowRollbackListDao.queryZpRecommendFlowRollbackListList(pmap);
    		if(flowList.size()<=2){
    			p.setFlowStatus((int)RecommendFlowType.COMPANY_INTERFACE_ROOLBACK.getCode());
        		ZpRecommendFlowRollbackList roll = new ZpRecommendFlowRollbackList();
        		roll.setFlowId(p.getAuditId());
        		roll.setJlId(p.getJlId());
        		roll.setCompanyId(p.getCompanyJobId());
        		roll.setCompanyName(p.getCompanyJobName());
        		roll.setCompanyJobId(p.getCompanyJobId());
        		roll.setJobName(p.getCompanyJobName());
        		roll.setMatchId(p.getMatchId());
        		roll.setRoolbackStatus((int)CompanyInterfaceRollbackType.WAIT_DEAL.getCode());
        		roll.setTechnicianAuditContent(p.getTechnicianAuditContent());
        		roll.setTechnicianAuditPerson(p.getTechnicianAuditPerson());
        		roll.setTechnicianAuditTime(p.getTechnicianAuditTime());
        		roll.setJlName(p.getJlName());
        		roll.setInTime(new Date());
        		roll.setInPerson(UserSessionUtils.getAccount().getAccountId());
        		roll.setInPersonName(UserSessionUtils.getAccount().getUsername());
        		
        		zpRecommendFlowRollbackListDao.addZpRecommendFlowRollbackList(roll);
        		
        		this.updateJlExpandFlowStatus(p.getJlId(), JlFlowType.PROCEED.getCode(), p.getMatchId());
        		
        		//更新流程状态
        		ZpRecommendFlowInfo p2 = new ZpRecommendFlowInfo();
        		p2.setAuditId(p.getAuditId());
        		p2.setFlowStatus(p.getFlowStatus());
        		this.updateZpRecommendFlowInfo(p2);
        		//发送邮件通知技术评审，重新填写推荐报告
        		sendMailToTechnicalAudit(p.getTechnicianAuditPerson(),p.getJlName());
        		return AjaxResponse.OK;
    		}
    		
     	
     	}
         catch (Exception e) {
 			e.printStackTrace();
 			 //失败
 	        return AjaxResponse.FAILED;
 		}
		return AjaxResponse.FAILED;
    }
    
    
    
    
    //企业对接人讲简历推荐到企业去
    public AjaxResponse recommandJlToCompany(ZpRecommendFlowInfo p){
    	try {
            if(UserSessionUtils.getAccount().getType()==AccountType.COMPANY_INTERFACER.getCode() || UserSessionUtils.getAccount().getType()==AccountType.ADMIN.getCode()){
         	   ZpRecommendFlowInfo newP = new ZpRecommendFlowInfo();
         	   newP.setAuditId(p.getAuditId());
         	   newP.setEnterpriseDockingPerson(UserSessionUtils.getAccount().getAccountId());
         	   //这个状态没用
         	   newP.setEnterpriseDockingStatus((int)RecommendFlowType.ALREADY_RECOMMEND_COMPANY.getCode());
         	   newP.setEnterpriseDockingTime(new Date());
         	   newP.setFlowStatus((int)RecommendFlowType.ALREADY_RECOMMEND_COMPANY.getCode());
    		   newP.setLastUpdateTime(DateUtil.getNow());
         	   this.zpRecommendFlowInfoDao.updateZpRecommendFlowInfo(newP);
            }
     		// 添加成功
     		return AjaxResponse.OK;
     	
     	}
         catch (Exception e) {
 			e.printStackTrace();
 			 //失败
 	        return AjaxResponse.FAILED;
 		}
    }
    
    //企业推荐反馈
    public AjaxResponse companyRecommandFeedback(ZpRecommendFlowInfo p){
    	try {
            if(UserSessionUtils.getAccount().getType()==AccountType.COMPANY_INTERFACER.getCode() || UserSessionUtils.getAccount().getType()==AccountType.ADMIN.getCode()){
               ZpRecommendFlowInfo newP = new ZpRecommendFlowInfo();
         	   newP.setAuditId(p.getAuditId());
         	   newP.setEnterpriseReplyStatus(p.getEnterpriseReplyStatus());
         	   newP.setEnterpriseReplyContent(p.getEnterpriseReplyContent());
         	   newP.setEnterpriseReplyTime(new Date());
    		   newP.setLastUpdateTime(DateUtil.getNow());

         	   if(p.getEnterpriseReplyStatus()==DictionaryUtil.getCode(DictionaryType.ENTERPRISE_REPLY_STATUS.getCode(), "邀请面试"))
         	   {
         		  //获取电话猎头对接人
         		  final Account account = this.getHeadhunterInterfacePerson();
         		  if(account==null){
                 	   return  new AjaxResponse(-1, "没找到猎头顾问");
                   }
         		  
         		 new Thread(){ 
               	  @Override 
               	  public void run() { 
               		  String nickname=account.getNickName();
               	       String email=account.getEmail();
               	        if(email!=null && StringUtil.isEmail(email) ){
               	         SimpleEmailVo vo = new SimpleEmailVo();
               	            vo.addEmail(email);
               	                     vo.setTitle("憬仪通知");
               	                     vo.setContent(ApplicationConst.getMessage("10103", nickname,String.valueOf("1")));
               	                     emailClient.send(vo);
               	                     log.info("发送时间："+DateUtil.toString(DateUtil.getNow(), DateUtil.DEFAULT_LONG_FORMAT));
               	        }
               	  } 
                   }.start();
             	   newP.setHeadhunterInterfacePerson(account.getAccountId());
         		  //企业反馈通过
         		  newP.setFlowStatus((int)RecommendFlowType.COMPANY_INVITATION_INTERVIEW.getCode());
         	   }else {
         		   //企业没通过
             	   newP.setFlowStatus((int)RecommendFlowType.RECOMMEND_COMPANY_FAILURE.getCode());
             	  
             	   //更新流程状态
        		   this.updateJlExpandFlowStatus(null,p.getAuditId(), JlFlowType.FLOW_END.getCode());
         	   }
    		   this.zpRecommendFlowInfoDao.updateZpRecommendFlowInfo(newP);
    		   
            }
     		// 添加成功
     		return AjaxResponse.OK;
     	}
         catch (Exception e) {
 			e.printStackTrace();
 			 //失败
 	        return AjaxResponse.FAILED;
 		}
    }
    
    //企业推荐反馈
    public AjaxResponse headhunterNotifyFeedback(ZpRecommendFlowInfo p){
    	try {
            if(UserSessionUtils.getAccount().getType()==AccountType.HEAD_HUNTING_MANAGER.getCode() || UserSessionUtils.getAccount().getType()==AccountType.ADMIN.getCode()){
               ZpRecommendFlowInfo newP = new ZpRecommendFlowInfo();
         	   newP.setAuditId(p.getAuditId());
         	   newP.setHrNoticeStatus(p.getHrNoticeStatus());
         	   newP.setHrNoticeFeedbackContent(p.getHrNoticeFeedbackContent());
         	   newP.setHrNoticeTime(new Date());
    		   newP.setLastUpdateTime(DateUtil.getNow());

         	   if(p.getHrNoticeStatus()==DictionaryUtil.getCode(DictionaryType.HEADHUNTER_REPLY_STATUS.getCode(), "已同意面试"))
         	   {
         		  //等待求职者去面试
         		  newP.setFlowStatus((int)RecommendFlowType.WAIT_JOBHUNTER_GOTO_INTERVIEW.getCode());
         	   }else {
          		  newP.setFlowStatus((int)RecommendFlowType.JOBHUNTER_NO_AGREE_INTERVIEW.getCode());
          		  //更新流程状态
       		      this.updateJlExpandFlowStatus(null,p.getAuditId(), JlFlowType.FLOW_END.getCode());

         	   }
    		   this.zpRecommendFlowInfoDao.updateZpRecommendFlowInfo(newP);
            }
     		// 添加成功
     		return AjaxResponse.OK;
     	}
         catch (Exception e) {
 			e.printStackTrace();
 			 //失败
 	        return AjaxResponse.FAILED;
 		}
    }
    
    //是否已经去面试的反馈
    public AjaxResponse isGotoInterviewFeedback(ZpRecommendFlowInfo p){
    	try {
            if(UserSessionUtils.getAccount().getType()==AccountType.HEAD_HUNTING_MANAGER.getCode() || UserSessionUtils.getAccount().getType()==AccountType.ADMIN.getCode()){
               ZpRecommendFlowInfo newP = new ZpRecommendFlowInfo();
         	   newP.setAuditId(p.getAuditId());
         	   newP.setHunterGotoInterviewOperatorTime(new Date());
         	   newP.setHunterGotoInterviewStatus(p.getHunterGotoInterviewStatus());
         	   newP.setHunterReplayContent(p.getHunterReplayContent());
         	   newP.setFlowStatus(p.getHunterGotoInterviewStatus());
    		   newP.setLastUpdateTime(DateUtil.getNow());

         	   this.zpRecommendFlowInfoDao.updateZpRecommendFlowInfo(newP);
    		   
    		   //未去面试，结束流程
    		  if(p.getHunterGotoInterviewStatus() == RecommendFlowType.HUNTER_NO_GOTO_INTERVIEW.getCode()){
    			//更新流程状态
       		    this.updateJlExpandFlowStatus(null,p.getAuditId(), JlFlowType.FLOW_END.getCode());
    		  }
    		  else {
    			  
    	    	ZpRecommendFlowInfo flow = this.zpRecommendFlowInfoDao.getZpRecommendFlowInfoById(p.getAuditId());

    			//插入登录人薪资记录
	    		ZpJlInfoService zpJlInfoService=(ZpJlInfoService) SpringBeanFactoryUtils.getBean("zpJlInfoService");
  		    	ZpJlInfo jlinfo = zpJlInfoService.getZpJlInfoById(flow.getJlId());
  		    	Account account=this.accountDao.getAccountById(jlinfo.getInPerson());
	  		    if(account.getType() == AccountType.HR.getCode() || account.getType() == AccountType.WEI_ACCOUNT.getCode()  )
	  		    {
	  		    	long type=DictionaryUtil.getCode(DictionaryType.SALARY_DEFINITION_TYPE.getCode(), Const.THREE);
	  		    	double  price=Double.parseDouble(DictionaryUtil.getBean(type).getValue());
	 	  		 	//添加用户薪水
	  		    	ZpAccountSalaryHistoryService zpAccountSalaryHistoryService=(ZpAccountSalaryHistoryService) SpringBeanFactoryUtils.getBean("zpAccountSalaryHistoryService");
   		    	    zpAccountSalaryHistoryService.addZpAccountSalaryHistory(UserSessionUtils.getAccount().getAccountId(),type, price);
	  	  		    //统计当前用户薪资统计信息,推送消息到前端
	  	  		    ChatAnnotation.sendToSingleForSalary(jlinfo.getInPerson());
	  		    }

    			  //添加微用户、群主、商务谈判、群外统计数量
    			  addStatisticsNum(p.getAuditId(), 1);
    		  }
    		 
            }
     		// 添加成功
     		return AjaxResponse.OK;
     	}
         catch (Exception e) {
 			e.printStackTrace();
 			 //失败
 	        return AjaxResponse.FAILED;
 		}
    }
    
     /**
      * //添加微用户、群主、商务谈判、群外统计数量
      * @param auditId 流程id
      * @param type 1 已去面试  2 面试已通过
      * @throws Exception
      */
     public void addStatisticsNum(Long auditId,int type) throws Exception{
    	 ZpRecommendFlowInfo b =(ZpRecommendFlowInfo) baseDao.getSimpleObjectByIdV1(ZpRecommendFlowInfo.class, auditId);
		    ZpJlExpandInfo c = new ZpJlExpandInfo();
		    c.setJlId(b.getJlId());
		    ZpJlExpandInfo d =(ZpJlExpandInfo) baseDao.getSimpleObjectV1(c);
		    if(d.getQqGroupId()!=null){
		    	ZpJlInfo e = new ZpJlInfo();
			    e.setJlId(b.getJlId());
			    ZpJlInfo f =(ZpJlInfo) baseDao.getSimpleObjectV1(e);
			    d.setQqGroupId(d.getQqGroupId());
			   //已去面试需要回写到统计信息表中
				ZpJlStatisticsInfo jsi = new ZpJlStatisticsInfo();
				jsi.setAccountType((int)AccountType.WEI_ACCOUNT.getCode());
				jsi.setGroupId(d.getQqGroupId());
				jsi.setInPerson(f.getInPerson());
				if(type==1){
					jsi.setInterviewCount(1);//去面试数，如果是新增为1
				}else
				if(type==2){
					jsi.setOfferCount(1);//面试成功，如果是新增为1
				}	
				zpJlStatisticsInfoService.addOrUpdateJlStatisticsInfo(jsi);
		   }
    	 
     }
    
    //面试的反馈
    public AjaxResponse interviewerFeedback(ZpRecommendFlowInfo p){
    	try {
            if(UserSessionUtils.getAccount().getType()==AccountType.HEAD_HUNTING_MANAGER.getCode() || UserSessionUtils.getAccount().getType()==AccountType.ADMIN.getCode()){
               ZpRecommendFlowInfo newP = new ZpRecommendFlowInfo();
         	   newP.setAuditId(p.getAuditId());
               newP.setHunterInterviewStatus(p.getHunterInterviewStatus());
         	   newP.setHunterInerviewReplayContent(p.getHunterInerviewReplayContent());
         	   newP.setHunterInterviewOperatorTime(new Date());
         	   newP.setFlowStatus(p.getHunterInterviewStatus());
    		   newP.setLastUpdateTime(DateUtil.getNow());
    		   //面试通过
     		  if(p.getHunterInterviewStatus() == RecommendFlowType.HUNTER_INTERVIEW_PASS.getCode()){
     			 //添加微用户、群主、商务谈判、群外统计数量
    			  addStatisticsNum(p.getAuditId(), 2);
     		  }

         	   this.zpRecommendFlowInfoDao.updateZpRecommendFlowInfo(newP);
         	   
     			//更新流程状态
        	   this.updateJlExpandFlowStatus(null,p.getAuditId(), JlFlowType.FLOW_END.getCode());
            }
     		// 添加成功
     		return AjaxResponse.OK;
     	}
         catch (Exception e) {
 			e.printStackTrace();
 			 //失败
 	        return AjaxResponse.FAILED;
 		}
    }
    
    //获取企业对接人
    public Account getCompanyInterfacePerson(){
		 HashMap<String,Object> pmap = new HashMap<String,Object>();
		 pmap.put("type", AccountType.COMPANY_INTERFACER.getCode());
		 pmap.put("isDelete", PauseStartType.START.getCode());
    	 List<Account> list = accountDao.queryAccountList(pmap);
    	 if(list!=null && list.size()>0){
    		return list.get(0);
    	 }
    	 return null;
    }
    
    public void sendMailToTechnicalAudit(Long accountId,final String jlName){
    	final Account account =getAccountById(accountId);
    	if(account!=null){
    		new Thread(){ 
          	  @Override 
          	  public void run() { 
          	       String email=account.getEmail();
          	        if(email!=null && StringUtil.isEmail(email) ){
          	         SimpleEmailVo vo = new SimpleEmailVo();
          	            vo.addEmail(email);
          	                     vo.setTitle("憬仪通知");
          	                     vo.setContent(ApplicationConst.getMessage("10107", jlName));
          	                     emailClient.send(vo);
          	                     log.info("发送时间："+DateUtil.toString(DateUtil.getNow(), DateUtil.DEFAULT_LONG_FORMAT));
          	        }
          	  } 
              }.start();
    		
    	}
    }
    
    //获取电话猎头对接人
    public Account getHeadhunterInterfacePerson(){
		 HashMap<String,Object> pmap = new HashMap<String,Object>();
		 pmap.put("type", AccountType.HEAD_HUNTING_MANAGER.getCode());
		 pmap.put("isDelete", PauseStartType.START.getCode());
    	 List<Account> list = accountDao.queryAccountList(pmap);
    	 if(list!=null && list.size()>0){
    		return list.get(0);
    	 }
    	 return null;
    }
    
    //更新流程状态
    public void updateJlExpandFlowStatus(Long jlId,Long auditId,int flowType) throws Exception{
    	HashMap<String,Object> pMap = new HashMap<String,Object>();

    	if(jlId==null) {
    		ZpRecommendFlowInfo flow = this.zpRecommendFlowInfoDao.getZpRecommendFlowInfoById(auditId);
    		jlId = flow.getJlId();
    	}else {
    		//技术评审是，简历id 不为null
    		pMap.put("auditTypeId", JlAuditType.AUDIT_OK.getCode());

    	}
    	
    	pMap.put("qtype", "one");
    	pMap.put("jlId", jlId);
    	pMap.put("recommendFlowStatus", flowType);
    	this.zpJlExpandInfoDao.updateZpJlExpandInfo(pMap);
    }
    
  //更新流程状态
    public void updateJlExpandFlowStatus(Long jlId,int flowType,Long matchId) throws Exception{
    	HashMap<String,Object> pMap = new HashMap<String,Object>();
    	pMap.put("auditTypeId", JlAuditType.COMPANY_INTERFACE_RECOMMEND_ROLLBACK.getCode());
    	pMap.put("qtype", "one");
    	pMap.put("jlId", jlId);
    	pMap.put("matchId", matchId);
    	pMap.put("recommendFlowStatus", flowType);
    	this.zpJlExpandInfoDao.updateZpJlExpandInfo(pMap);
    }
    
    public Account getAccountById(long accountId){
    	HashMap<String,Object> pmap = new HashMap<String,Object>();
		 pmap.put("type", AccountType.TECHICAL_AUDIT.getCode());
		 pmap.put("isDelete", PauseStartType.START.getCode());
		 pmap.put("accountId", accountId);
		 List<Account> list = accountDao.queryAccountList(pmap);
    	 if(list!=null && list.size()>0){
    		return list.get(0);
    	 }
    	 return null;
    }
    
    public ZpRecommendFlowInfo getFlowId(ZpRecommendFlowInfo  p) throws Exception{
		 HashMap<String,Object> pmap = new HashMap<String,Object>();
		 pmap.put("jlId", p.getJlId());
		 pmap.put("matchId", p.getMatchId());
		 return this.getZpRecommendFlowInfo(pmap);
    }
    
    
}
